/**
 ******************************************************************************
 * @file           : custom_bus.c
 * @brief          : source file for the BSP BUS IO driver
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under Ultimate Liberty license
 * SLA0044, the "License"; You may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 *                             www.st.com/SLA0044
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "custom_bus.h"

__weak HAL_StatusTypeDef MX_I2C1_Init(I2C_HandleTypeDef *hi2c);

#if defined (STM32L031xx)
#define I2C1_AF_NUM GPIO_AF1_I2C1
#endif
// #if defined (STM32G070xx)
// #define I2C1_AF_NUM GPIO_AF6_I2C1
// #endif
// #if defined (STM32G030xx)
// #define I2C1_AF_NUM GPIO_AF6_I2C1
// #endif
// #if defined (STM32GB1xx)
// #define I2C1_AF_NUM GPIO_AF6_I2C1
// #endif
#if defined (STM32G0xx)
#define I2C1_AF_NUM GPIO_AF6_I2C1
#endif
/** @addtogroup BSP
 * @{
 */

/** @addtogroup CUSTOM
 * @{
 */

/** @defgroup CUSTOM_BUS CUSTOM BUS
 * @{
 */

/** @defgroup CUSTOM_BUS_Exported_Variables BUS Exported Variables
 * @{
 */

I2C_HandleTypeDef hi2c1;
/**
 * @}
 */

/** @defgroup CUSTOM_BUS_Private_Variables BUS Private Variables
 * @{
 */

#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1U)
static uint32_t IsI2C1MspCbValid = 0;
#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
static uint32_t I2C1InitCounter = 0;

/**
 * @}
 */

/** @defgroup CUSTOM_BUS_Private_FunctionPrototypes  BUS Private Function
 * @{
 */

static void I2C1_MspInit(I2C_HandleTypeDef *hI2c);
static void I2C1_MspDeInit(I2C_HandleTypeDef *hI2c);
#if (USE_CUBEMX_BSP_V2 == 1)
static uint32_t I2C_GetTiming(uint32_t clock_src_hz, uint32_t i2cfreq_hz);
static void Compute_PRESC_SCLDEL_SDADEL(uint32_t clock_src_freq, uint32_t I2C_Speed);
static uint32_t Compute_SCLL_SCLH(uint32_t clock_src_freq, uint32_t I2C_speed);
#endif

/**
 * @}
 */

/** @defgroup CUSTOM_LOW_LEVEL_Private_Functions CUSTOM LOW LEVEL Private Functions
 * @{
 */

/** @defgroup CUSTOM_BUS_Exported_Functions CUSTOM_BUS Exported Functions
 * @{
 */

/* BUS IO driver over I2C Peripheral */
/*******************************************************************************
                            BUS OPERATIONS OVER I2C
*******************************************************************************/
/**
 * @brief  Initialize I2C HAL
 * @retval BSP status
 */
int32_t BSP_I2C1_Init(void)
{

    int32_t ret = BSP_ERROR_NONE;

    hi2c1.Instance = I2C1;

    if (I2C1InitCounter++ == 0)
    {
        if (HAL_I2C_GetState(&hi2c1) == HAL_I2C_STATE_RESET)
        {
#if (USE_HAL_I2C_REGISTER_CALLBACKS == 0U)
            /* Init the I2C Msp */
            I2C1_MspInit(&hi2c1);
#else
            if (IsI2C1MspCbValid == 0U)
            {
                if (BSP_I2C1_RegisterDefaultMspCallbacks() != BSP_ERROR_NONE)
                {
                    return BSP_ERROR_MSP_FAILURE;
                }
            }
#endif
            if (ret == BSP_ERROR_NONE)
            {
                /* Init the I2C */
                if (MX_I2C1_Init(&hi2c1) != HAL_OK)
                {
                    ret = BSP_ERROR_BUS_FAILURE;
                }
                else if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
                {
                    ret = BSP_ERROR_BUS_FAILURE;
                }
                else
                {
                    ret = BSP_ERROR_NONE;
                }
            }
        }
    }
    return ret;
}

/**
 * @brief  DeInitialize I2C HAL.
 * @retval BSP status
 */
int32_t BSP_I2C1_DeInit(void)
{
    int32_t ret = BSP_ERROR_NONE;

    if (I2C1InitCounter > 0)
    {
        if (--I2C1InitCounter == 0)
        {
#if (USE_HAL_I2C_REGISTER_CALLBACKS == 0U)
            /* DeInit the I2C */
            I2C1_MspDeInit(&hi2c1);
#endif
            /* DeInit the I2C */
            if (HAL_I2C_DeInit(&hi2c1) != HAL_OK)
            {
                ret = BSP_ERROR_BUS_FAILURE;
            }
        }
    }
    return ret;
}

/**
 * @brief  Check whether the I2C bus is ready.
 * @param DevAddr : I2C device address
 * @param Trials : Check trials number
 *	@retval BSP status
 */
int32_t BSP_I2C1_IsReady(uint16_t DevAddr, uint32_t Trials)
{
    int32_t ret = BSP_ERROR_NONE;

    if (HAL_I2C_IsDeviceReady(&hi2c1, DevAddr, Trials, BUS_I2C1_POLL_TIMEOUT) != HAL_OK)
    {
        ret = BSP_ERROR_BUSY;
    }

    return ret;
}

/**
 * @brief  Write a value in a register of the device through BUS.
 * @param  DevAddr Device address on Bus.
 * @param  Reg    The target register address to write
 * @param  pData  Pointer to data buffer to write
 * @param  Length Data Length
 * @retval BSP status
 */

int32_t BSP_I2C1_WriteReg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
{
    int32_t ret = BSP_ERROR_NONE;

    if (HAL_I2C_Mem_Write(&hi2c1, DevAddr, Reg, I2C_MEMADD_SIZE_8BIT, pData, Length, BUS_I2C1_POLL_TIMEOUT) != HAL_OK)
    {
        if (HAL_I2C_GetError(&hi2c1) == HAL_I2C_ERROR_AF)
        {
            ret = BSP_ERROR_BUS_ACKNOWLEDGE_FAILURE;
        }
        else
        {
            ret = BSP_ERROR_PERIPH_FAILURE;
        }
    }
    return ret;
}

/**
 * @brief  Read a register of the device through BUS
 * @param  DevAddr Device address on Bus.
 * @param  Reg    The target register address to read
 * @param  pData  Pointer to data buffer to read
 * @param  Length Data Length
 * @retval BSP status
 */
int32_t BSP_I2C1_ReadReg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
{
    int32_t ret = BSP_ERROR_NONE;

    if (HAL_I2C_Mem_Read(&hi2c1, DevAddr, Reg, I2C_MEMADD_SIZE_8BIT, pData, Length, BUS_I2C1_POLL_TIMEOUT) != HAL_OK)
    {
        if (HAL_I2C_GetError(&hi2c1) == HAL_I2C_ERROR_AF)
        {
            ret = BSP_ERROR_BUS_ACKNOWLEDGE_FAILURE;
        }
        else
        {
            ret = BSP_ERROR_PERIPH_FAILURE;
        }
    }
    return ret;
}

/**

  * @brief  Write a value in a register of the device through BUS.
  * @param  DevAddr Device address on Bus.
  * @param  Reg    The target register address to write

  * @param  pData  Pointer to data buffer to write
  * @param  Length Data Length
  * @retval BSP statu
  */
int32_t BSP_I2C1_WriteReg16(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
{
    int32_t ret = BSP_ERROR_NONE;

    if (HAL_I2C_Mem_Write(&hi2c1, DevAddr, Reg, I2C_MEMADD_SIZE_16BIT, pData, Length, BUS_I2C1_POLL_TIMEOUT) != HAL_OK)
    {
        if (HAL_I2C_GetError(&hi2c1) == HAL_I2C_ERROR_AF)
        {
            ret = BSP_ERROR_BUS_ACKNOWLEDGE_FAILURE;
        }
        else
        {
            ret = BSP_ERROR_PERIPH_FAILURE;
        }
    }
    return ret;
}

/**
 * @brief  Read registers through a bus (16 bits)
 * @param  DevAddr: Device address on BUS
 * @param  Reg: The target register address to read
 * @param  Length Data Length
 * @retval BSP status
 */
int32_t BSP_I2C1_ReadReg16(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
{
    int32_t ret = BSP_ERROR_NONE;

    if (HAL_I2C_Mem_Read(&hi2c1, DevAddr, Reg, I2C_MEMADD_SIZE_16BIT, pData, Length, BUS_I2C1_POLL_TIMEOUT) != HAL_OK)
    {
        if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
        {
            ret = BSP_ERROR_BUS_ACKNOWLEDGE_FAILURE;
        }
        else
        {
            ret = BSP_ERROR_PERIPH_FAILURE;
        }
    }
    return ret;
}

/**
 * @brief  Send an amount width data through bus (Simplex)
 * @param  DevAddr: Device address on Bus.
 * @param  pData: Data pointer
 * @param  Length: Data length
 * @retval BSP status
 */
int32_t BSP_I2C1_Send(uint16_t DevAddr, uint8_t *pData, uint16_t Length)
{
    int32_t ret = BSP_ERROR_NONE;

    if (HAL_I2C_Master_Transmit(&hi2c1, DevAddr, pData, Length, BUS_I2C1_POLL_TIMEOUT) != HAL_OK)
    {
        if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
        {
            ret = BSP_ERROR_BUS_ACKNOWLEDGE_FAILURE;
        }
        else
        {
            ret = BSP_ERROR_PERIPH_FAILURE;
        }
    }

    return ret;
}

/**
 * @brief  Receive an amount of data through a bus (Simplex)
 * @param  DevAddr: Device address on Bus.
 * @param  pData: Data pointer
 * @param  Length: Data length
 * @retval BSP status
 */
int32_t BSP_I2C1_Recv(uint16_t DevAddr, uint8_t *pData, uint16_t Length)
{
    int32_t ret = BSP_ERROR_NONE;

    if (HAL_I2C_Master_Receive(&hi2c1, DevAddr, pData, Length, BUS_I2C1_POLL_TIMEOUT) != HAL_OK)
    {
        if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
        {
            ret = BSP_ERROR_BUS_ACKNOWLEDGE_FAILURE;
        }
        else
        {
            ret = BSP_ERROR_PERIPH_FAILURE;
        }
    }
    return ret;
}

#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1U)
/**
 * @brief Register Default BSP I2C1 Bus Msp Callbacks
 * @retval BSP status
 */
int32_t BSP_I2C1_RegisterDefaultMspCallbacks(void)
{

    __HAL_I2C_RESET_HANDLE_STATE(&hi2c1);

    /* Register MspInit Callback */
    if (HAL_I2C_RegisterCallback(&hi2c1, HAL_I2C_MSPINIT_CB_ID, I2C1_MspInit) != HAL_OK)
    {
        return BSP_ERROR_PERIPH_FAILURE;
    }

    /* Register MspDeInit Callback */
    if (HAL_I2C_RegisterCallback(&hi2c1, HAL_I2C_MSPDEINIT_CB_ID, I2C1_MspDeInit) != HAL_OK)
    {
        return BSP_ERROR_PERIPH_FAILURE;
    }
    IsI2C1MspCbValid = 1;

    return BSP_ERROR_NONE;
}

/**
 * @brief BSP I2C1 Bus Msp Callback registering
 * @param Callbacks     pointer to I2C1 MspInit/MspDeInit callback functions
 * @retval BSP status
 */
int32_t BSP_I2C1_RegisterMspCallbacks(BSP_I2C_Cb_t *Callbacks)
{
    /* Prevent unused argument(s) compilation warning */
    __HAL_I2C_RESET_HANDLE_STATE(&hi2c1);

    /* Register MspInit Callback */
    if (HAL_I2C_RegisterCallback(&hi2c1, HAL_I2C_MSPINIT_CB_ID, Callbacks->pMspInitCb) != HAL_OK)
    {
        return BSP_ERROR_PERIPH_FAILURE;
    }

    /* Register MspDeInit Callback */
    if (HAL_I2C_RegisterCallback(&hi2c1, HAL_I2C_MSPDEINIT_CB_ID, Callbacks->pMspDeInitCb) != HAL_OK)
    {
        return BSP_ERROR_PERIPH_FAILURE;
    }

    IsI2C1MspCbValid = 1;

    return BSP_ERROR_NONE;
}
#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */

/**
 * @brief  Return system tick in ms
 * @retval Current HAL time base time stamp
 */
int32_t BSP_GetTick(void)
{
    return HAL_GetTick();
}

/* I2C1 init function */

__weak HAL_StatusTypeDef MX_I2C1_Init(I2C_HandleTypeDef *hi2c)
{
    HAL_StatusTypeDef ret = HAL_OK;
    hi2c->Instance = I2C1;
    // hi2c->Init.Timing = 0x0010061A;
    hi2c1.Init.Timing = 0x00000107;
    hi2c->Init.OwnAddress1 = 0;
    hi2c->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c->Init.OwnAddress2 = 0;
    hi2c->Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    hi2c->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
    if (HAL_I2C_Init(hi2c) != HAL_OK)
    {
        ret = HAL_ERROR;
    }

    if (HAL_I2CEx_ConfigAnalogFilter(hi2c, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
    {
        ret = HAL_ERROR;
    }

    if (HAL_I2CEx_ConfigDigitalFilter(hi2c, 0) != HAL_OK)
    {
        ret = HAL_ERROR;
    }

    return ret;
}

static void I2C1_MspInit(I2C_HandleTypeDef *i2cHandle)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    /* USER CODE BEGIN I2C1_MspInit 0 */

    /* USER CODE END I2C1_MspInit 0 */

    BUS_I2C1_SCL_GPIO_CLK_ENABLE();
    BUS_I2C1_SDA_GPIO_CLK_ENABLE();

    GPIO_InitStruct.Pin = BUS_I2C1_SCL_GPIO_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = I2C1_AF_NUM;
    HAL_GPIO_Init(BUS_I2C1_SCL_GPIO_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = BUS_I2C1_SDA_GPIO_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = I2C1_AF_NUM;
    HAL_GPIO_Init(BUS_I2C1_SDA_GPIO_PORT, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __HAL_RCC_I2C1_CLK_ENABLE();
    /* USER CODE BEGIN I2C1_MspInit 1 */

    /* USER CODE END I2C1_MspInit 1 */
}

static void I2C1_MspDeInit(I2C_HandleTypeDef *i2cHandle)
{
    /* USER CODE BEGIN I2C1_MspDeInit 0 */

    /* USER CODE END I2C1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_I2C1_CLK_DISABLE();

    /**I2C1 GPIO Configuration
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA
    */
    // HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
    HAL_GPIO_DeInit(BUS_I2C1_SDA_GPIO_PORT, BUS_I2C1_SDA_GPIO_PIN);
    HAL_GPIO_DeInit(BUS_I2C1_SDA_GPIO_PORT, BUS_I2C1_SDA_GPIO_PIN);

    /* USER CODE BEGIN I2C1_MspDeInit 1 */

    /* USER CODE END I2C1_MspDeInit 1 */
}

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
